package com.windmt.controller;

import com.windmt.feign.HelloRemote;
import lombok.extern.apachecommons.CommonsLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author: yibo
 **/
@Slf4j
@RequestMapping("/hello")
@RestController
public class HelloController {

    @Autowired
    HelloRemote helloRemote;

    @GetMapping("/{name}")
    public String index(@PathVariable("name") String name, HttpServletRequest request) throws InterruptedException {

        String token = request.getHeader("token");
        log.info("header params, token is {}", token);
        String userId = request.getParameter("userId");
        log.info("query params, userId is {}", userId);

        log.info("the name is " + name);
        switch (name) {
            case "null":
                throw new NullPointerException();
            case "retry":
                TimeUnit.MINUTES.sleep(10);
                break;
        }
        return helloRemote.hello(name) + "\n" + new Date().toString();
    }


    @PostMapping("/post")
    public String post(@RequestBody Map<String, String> body) {
        log.info("body params is {}", body.toString());
        return helloRemote.post(body);
    }
}
